package
{

	import com.greensock.easing.Strong;

	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;

	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;

	import utils.Res;
	import utils.UIHelper;
	import utils.events.MenuEvent;

	[SWF(backgroundColor="0xffffff", width="600", height="700")]
	public class testa extends Sprite
	{

		public var roolBar1:Sprite=new Sprite();
		public var timer:Timer;
		private var resDomain:ApplicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);
		private var boolInit:Boolean;
		public var menuArrays:Array=[];
		private var menuSartX:Number=100;
		private var menuSartY:Number=0;
		private var menuContentX:Number=400;
		private var menuContentY:Number=0;
		private var lastMenuX:int;
		private var lastMenuY:Number=0;
		private var stopPassImgCount:int=3; //停止命令接到后，滚轮需要pass掉的图片数
		private var intervalTimer:Number=500; //定时器的周期
		private var betThumbnailsArray:Array=[[], [], []]; //第一列
		private var betThumbnails:Array=[]; //第一列
		private var betThumbnails2:Array=[]; //第二列
		private var betThumbnails3:Array=[];
		private var reelsContainer:Sprite=new Sprite(); //5个滚轮的容器
		private var rancolor:uint=0x000000; //
		private var stopflag:int; //是正在处理停止
		private var reelsCount:int=10; //图片个数
		private var currentSpeed:Number=0; //最大速度
		private var maxSpeed:Number; //最大速度
		private var currentBottom:int; //当前地下
		private var startY:int=100; //屏幕起始y
		private var ScreenHeight:int=300; //屏幕高度
		private var totalway:Number=0; //总共需要移动的距离
		private var currentStopTimerCount:int; //从停止开始，当前滚柱经过的定时器次数
		private var totalStopTimerCount:int; //总共停止，需要的次数
		private var rollstartinterval:Number=1000; //开始，滚柱间隔
		private var rollstopinterval:Number=1000; //停止，滚柱间隔
		private var whenstopTimerReapeat:Number=0;
		private var boolStopAccelerate:Boolean=false;
		private var speedstopAccelerate:Number=0;


		public function testa()
		{
			super();
			//initialSprite();
			//testGraphics();
			//testResTimer();
			//testMath();
			//loadRes2();		
			testSport();
		}

		private function testSport():void
		{
			// TODO Auto Generated method stub
			this.stage.scaleMode=StageScaleMode.NO_SCALE;
			rancolor+=50;
			UIHelper.addRectSpriteChild(this, null, null, 0, 100, 500, 300, 0xFF83FA, 1);
			//return;
			UIHelper.addRectSpriteChild(this, reelsContainer, null, 100, 100, 100, 0, rancolor, 1);
			for (var i:int=0; i < reelsCount; i++)
			{
				rancolor+=100;
				betThumbnails.push(UIHelper.addRectSpriteChild(reelsContainer, null, "reela" + i, 0, (4 - i) * 100, 100, 100, rancolor, 1));
				betThumbnails2.push(UIHelper.addRectSpriteChild(reelsContainer, null, "reelb" + i, 100, (4 - i) * 100, 100, 100, rancolor, 1));
//				betThumbnails3.push(UIHelper.addRectSpriteChild(reelsContainer, null, "reel3_" + i, 200, (4 - i) * 100, 100, 100, rancolor, 1));
			}
			for (var j:int=0; j < reelsCount; j++)
			{
				UIHelper.addTextFieldChild(betThumbnails[j], "reela" + j, null, 0, 0);
				UIHelper.addTextFieldChild(betThumbnails2[j], "reelb" + j, null, 0, 0);
//				UIHelper.addTextFieldChild(betThumbnails3[i], "reel3_" + i, null, 0, 0);
					//betThumbnails.push(UIHelper.addRectSpriteChild(reelsContainer, null, "reel"+i, 0, reelsContainer.height, 100, 100, rancolor, 1));
			}
			for (var k:int=0; k < reelsCount; k++)
			{
				var mysprite:Sprite=betThumbnails[k] as Sprite;
				trace(mysprite.name + "mysprite.y+reelsContainer.y:" + (mysprite.y + reelsContainer.y));
			}
			maxSpeed=100 / 2;
			currentSpeed=100 / 2;
			trace("想每帧滚动半张图：必须滚动速度：" + currentSpeed);
			totalStopTimerCount=8000 / this.intervalTimer;
			trace("想在8秒停止滚轮，必须计时器repeat次数：" + totalStopTimerCount + " ,想滚动" + stopPassImgCount + "个图片次停，最慢速度，" + ((stopPassImgCount * betThumbnails[0].height) / totalStopTimerCount + "最快速度，" + ((stopPassImgCount + 1) * betThumbnails[0].height) / totalStopTimerCount) + " 移动位移：" + stopPassImgCount * betThumbnails[0].height + " to " + (stopPassImgCount + 1) * betThumbnails[0].height);
			UIHelper.addBitmapDataButtonChild(this, Btn_Desk_Down, Btn_Desk_Down, onStartClickHandler, 479, 3, true);
			UIHelper.addBitmapDataButtonChild(this, Btn_Desk_Up, Btn_Desk_Up, onStopClickHandler, 479, 100, true);
			UIHelper.addBitmapDataButtonChild(this, Btn_Desk_Up, Btn_Desk_Up, onTweenClickHandler, 479, 600, true);
			trace("起始,滚柱之间的timer次数间隔：" + (rollstartinterval / this.intervalTimer));
			trace("停止滚柱之间的timer次数间隔：" + (this.rollstopinterval / this.intervalTimer));

		}


		protected var boolDoStartState:Array=[false, false, false];
		protected var boolDoStopState:Array=[false, false, false];

		protected function onSportTimerHandler(event:TimerEvent):void
		{
			// TODO Auto-generated method stub
			for (var i:int=1; i < 4; i++)
			{
				if (!boolDoStartState[i - 1])
				{

					if (this.timer.currentCount > (i - 1) * (rollstartinterval / this.intervalTimer))
					{
						boolDoStartState[i - 1]=true;
					}
				}
				if (boolDoStartState[i - 1])
				{
					runroll(i);
				}
				break;
			}

		}

		private function runroll(index:int):void
		{
			// TODO Auto Generated method stub
			if (stopflag == 1)
			{
				stopTimer();
				doTween();
				return;
			}
			if (stopflag == 1)
			{
				if (totalway <= 0)
				{
					trace("最底下的图片y坐标 " + (betThumbnails[currentBottom].y + reelsContainer.y - betThumbnails[currentBottom].height) + " betThumbnails[currentBottom].y:" + betThumbnails[currentBottom].y + " reelsContainer.y:" + reelsContainer.y);
					totalway=this.startY + this.ScreenHeight - betThumbnails[currentBottom].y - reelsContainer.y + (stopPassImgCount) * betThumbnails[currentBottom].height;
					var lastbottom:int=currentBottom + stopPassImgCount + 1;
					lastbottom=lastbottom > reelsCount - 1 ? lastbottom - reelsCount : lastbottom;
					//lastbottom=lastbottom >= reelsCount - 1 ? lastbottom - reelsCount + 1 : lastbottom;
					trace("when stop ,currentBottom " + currentBottom + "roll must go way:" + totalway + " and will last bottom:" + lastbottom);
					if (boolStopAccelerate)
					{
						//减速
						currentSpeed=2 * totalway / totalStopTimerCount;
						speedstopAccelerate=currentSpeed / totalStopTimerCount;
						trace("做减速停止，v：" + currentSpeed + " 减速度：" + speedstopAccelerate);
					}
					else
					{
						//匀速
						currentSpeed=totalway / totalStopTimerCount;
					}

					trace("need roll per timer,speedMax " + currentSpeed);
				}
				currentStopTimerCount++;
				if (boolStopAccelerate)
				{
					//if (currentStopTimerCount > 1)
					currentSpeed-=speedstopAccelerate;
				}

				trace("当前currentStopTimerCount：" + currentStopTimerCount);
				//如果剩余的路程，或者第几次开始，将谜底更换背景图
				if (currentStopTimerCount - 1 >= this.totalStopTimerCount)
				{
					totalway=0;
					currentStopTimerCount=0;
					//currentBottom=currentBottom == reelsCount - 1 ? 0 : currentBottom + 1;
					trace("定时器已经走动：" + totalStopTimerCount + " 停止时候当前最低索引：" + currentBottom);
					trace("停止时候parent x,y,h,w " + reelsContainer.x + " " + reelsContainer.y + " " + reelsContainer.height + " " + reelsContainer.width);
					trace("停止时，最底下 child x,y,h,w " + betThumbnails[currentBottom].x + " " + betThumbnails[currentBottom].y + " " + betThumbnails[currentBottom].height + " " + betThumbnails[currentBottom].width);
					stopTimer();
					return;
				}
			}
			//trace(timer.currentCount + ":" + timer.repeatCount);
			var bottom:Sprite=betThumbnails[i] as Sprite;
			for (var i:int=0; i < betThumbnails.length; i++)
			{
				var mysprite:Sprite=betThumbnails[i] as Sprite;
				mysprite.y+=currentSpeed;
					//trace("parent x,y,h,w " + reelsContainer.x + " " + reelsContainer.y + " " + reelsContainer.height + " " + reelsContainer.width);
					//trace("child x,y,h,w " + mysprite.x + " " + mysprite.y + " " + mysprite.height + " " + mysprite.width);
					//ace(mysprite.name + " mysprite.y+reelsContainer.y" + (mysprite.y + reelsContainer.y));
			}
			if (currentStopTimerCount == totalStopTimerCount)
			{
				trace("最后一次移动！,底部索引：" + currentBottom);
				trace("最后一次移动，底部图片信息，最底下 child x,y,h,w " + betThumbnails[currentBottom].x + " " + betThumbnails[currentBottom].y + " " + betThumbnails[currentBottom].height + " " + betThumbnails[currentBottom].width);
			}
			changeBottomIndex();

		}

		private function changeBottomIndex():void
		{
			// TODO Auto Generated method stub
			for (var j:int=currentBottom; j < betThumbnails.length; j++)
			{
				if (betThumbnails[j].y + reelsContainer.y - this.startY - this.ScreenHeight >= 0)
				{
					var preIndex:int=j > 0 ? j - 1 : reelsCount - 1;
					trace(j + "上一个是 " + preIndex);
					betThumbnails[j].y=betThumbnails[preIndex].y - betThumbnails[j].height;
					currentBottom=j == reelsCount - 1 ? 0 : j + 1;
					trace("更换了底部currentBottom:" + currentBottom);
				}
				else
				{
					break;
				}

			}
		}

		private function doTween():void
		{
			// TODO Auto Generated method stub
			totalway=this.startY + this.ScreenHeight - betThumbnails[currentBottom].y - reelsContainer.y + (stopPassImgCount) * betThumbnails[currentBottom].height;
			var lastbottom:int=currentBottom + stopPassImgCount + 1;
			lastbottom=lastbottom > reelsCount - 1 ? lastbottom - reelsCount : lastbottom;
			//lastbottom=lastbottom >= reelsCount - 1 ? lastbottom - reelsCount + 1 : lastbottom;
			trace("when stop ,currentBottom " + currentBottom + "roll must go way:" + totalway + " and will last bottom:" + lastbottom);
			var myTween:Tween=new Tween(this.reelsContainer, "y", Strong.easeIn, this.reelsContainer.y, this.reelsContainer.y + totalway, 1, true);
			myTween.addEventListener(TweenEvent.MOTION_FINISH, onTweenFinishHanlder);
			myTween.start();
		}

		private function doTween2():void
		{
			// TODO Auto Generated method stub
			totalway=this.startY + this.ScreenHeight - betThumbnails[currentBottom].y - reelsContainer.y + (stopPassImgCount) * betThumbnails[currentBottom].height;
			var lastbottom:int=currentBottom + stopPassImgCount + 1;
			lastbottom=lastbottom > reelsCount - 1 ? lastbottom - reelsCount : lastbottom;
			for (var i:int=0; i < betThumbnails.length; i++)
			{
				var mysprite:Sprite=betThumbnails[i] as Sprite;
				mysprite.y+=totalway;
			}
			//lastbottom=lastbottom >= reelsCount - 1 ? lastbottom - reelsCount + 1 : lastbottom;
			trace("when stop ,currentBottom " + currentBottom + "roll must go way:" + totalway + " and will last bottom:" + lastbottom);
			var myTween:Tween=new Tween(this.reelsContainer, "y", Strong.easeIn, this.reelsContainer.y - totalway, this.reelsContainer.y, 2, true);
			myTween.addEventListener(TweenEvent.MOTION_FINISH, onTweenFinishHanlder);
			myTween.start();
		}

		protected function onTweenFinishHanlder(event:TweenEvent):void
		{
			// TODO Auto-generated method stub
			trace("我已经Tween结束");
			trace("开始更换底部图");
			changeBottomIndex();
			trace("Tween 结束时候当前最低索引：" + currentBottom);
			trace("Tween 结束时候parent x,y,h,w " + reelsContainer.x + " " + reelsContainer.y + " " + reelsContainer.height + " " + reelsContainer.width);
			trace("Tween 结束时，最底下 child x,y,h,w " + betThumbnails[currentBottom].x + " " + betThumbnails[currentBottom].y + " " + betThumbnails[currentBottom].height + " " + betThumbnails[currentBottom].width);
		}

		private function onTweenClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub
			trace("Tween 停止时候当前最低索引：" + currentBottom);
			trace("Tween 停止时候parent x,y,h,w " + reelsContainer.x + " " + reelsContainer.y + " " + reelsContainer.height + " " + reelsContainer.width);
			trace("Tween 停止时，最底下 child x,y,h,w " + betThumbnails[currentBottom].x + " " + betThumbnails[currentBottom].y + " " + betThumbnails[currentBottom].height + " " + betThumbnails[currentBottom].width);
			doTween2();
		}

		private function onStartClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub		
			startSportTimer();
			stopflag=0;
		}

		private function onStopClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub
			stopflag=1;
			this.whenstopTimerReapeat=this.timer.repeatCount;
			stopTimer();
		}

		private function stopSportTimer():void
		{
			// TODO Auto Generated method stub
			trace("Timer stop");
			if (timer != null)
			{
				if (timer.running)
				{
					timer.stop();
					timer.reset();
				}
				timer.removeEventListener(TimerEvent.TIMER, this.onSportTimerHandler);
			}
		}

		private function startSportTimer():void
		{
			// TODO Auto Generated method stub
			trace("Timer start");
			if (timer == null)
			{
				timer=new Timer(intervalTimer);
			}
			if (!timer.running)
			{
				timer.addEventListener(TimerEvent.TIMER, onSportTimerHandler);
				timer.start();
			}
		}

		protected function onLoaderRes2CompleteHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			boolInit=true;
			//		  this.addChild(new Bitmap(Res.get("betperline_up")));
			//		  this.addChild(new Bitmap(Res.get("spin_up")));
			//testDynamicBtn();
			//测试按钮
			//testMenu();
			//测试位图点击
			//			testBitMap();
			//遮罩文字
			//testTextField();
			//testCustomSprite();
		}






		private function testTimer():void
		{
			// TODO Auto Generated method stub
			var btn_start:SimpleButton=new SimpleButton();
			var upshape:Shape=new Shape;
			upshape.graphics.beginFill(0x003232);
			upshape.graphics.drawRect(0, 0, 20, 20);
			upshape.graphics.endFill();
			var downshape:Shape=new Shape;
			downshape.graphics.beginFill(0x000000);
			downshape.graphics.drawRect(0, 0, 20, 20);
			downshape.graphics.endFill();
			btn_start.upState=upshape;
			btn_start.downState=downshape;
			btn_start.overState=upshape;
			btn_start.hitTestState=upshape;
			btn_start.addEventListener(MouseEvent.CLICK, onStartHandler);
			this.addChild(btn_start);

			var btn_sop:SimpleButton=new SimpleButton();
			btn_sop.x=100;
			btn_sop.y=100;
			btn_sop.upState=new Bitmap(Res.get("gun"));
			btn_sop.downState=new Bitmap(Res.get("female"));
			;
			btn_sop.overState=new Bitmap(Res.get("jewelry"));
			;
			btn_sop.hitTestState=new Bitmap(Res.get("knife"));
			;
			btn_sop.addEventListener(MouseEvent.CLICK, onStopHandler);
			this.addChild(btn_sop);
			var gun:Bitmap=new Bitmap(Res.get("gun"));
			gun.smoothing=true;
			gun.width=200;
			gun.height=200;
			this.addChild(gun);

		}

		protected function onStopHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			trace("stop timer");
			stopTimer();

		}

		private function stopTimer():void
		{
			// TODO Auto Generated method stub
			if (timer != null)
			{
				if (timer.running)
				{
					timer.stop();
				}
				timer.removeEventListener(TimerEvent.TIMER, this.onTimerHandler);
			}
		}

		private function startTimer():void
		{
			// TODO Auto Generated method stub
			if (timer == null)
			{
				timer=new Timer(1000);
			}
			if (!timer.running)
			{
				timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
				timer.start();
			}
		}

		protected function onStartHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			trace("start timer");
			startTimer();
		}

		protected function onTimerHandler(event:TimerEvent):void
		{
			// TODO Auto-generated method stub
			trace(timer.currentCount + ":" + timer.repeatCount);
		}

	}
}
